package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

@Slf4j
@RestController
@RequestMapping("/build")
public class BuildController {
    @Value("${server.build.timestamp:}")
    private String buildTimeStamp;
    @Value("${server.build.format:}")
    private String buildTimeformat;

    private static String version = "V1.0 Build ";
    private static String buildTime = null;
    @GetMapping("/version")
    public String version() {
        if(StringUtils.isNotEmpty(buildTime)) {
            return version;
        }
        try {
            SimpleDateFormat df1 = new SimpleDateFormat(buildTimeformat);
            df1.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date parse = df1.parse(buildTimeStamp);
            buildTime = new SimpleDateFormat("yyyyMMddHHmmss").format(parse);
            version += buildTime;
        } catch (ParseException e) {
            log.error("获取版本异常",e);
        }
        return version;
    }
}
